function fsb_functional_map(idat,inparg,sandbox)

% FSB: Draw a functional map into separate figure
%
% EXAMPLE:
% fsb_functional_map(idat,inparg)
%
% INPUT:
% idat: 3D image data
% inparg: input argument struct with fields
% inparg.newmap: Should a new figure be drawn (1)
% inparg.map_txt: Title of figure
% inparg.nscal: Should the image be scaled (1)
% inparg.hoverlay: High resolution overlay (1)
% inparg.calcorr: Should maps be shown (1)
% inparg.corparam: Correlation threshold
% inparg.probparam: Probability threshold
% inparg.rc : Which map should be shown?
%             correlation (1)
%             probability (0)
%
%
% OUTPUT:
% functional map overlaid on structural/diagnostical images
%
% CALLED BY:
% fsb_diag.m
%
% NOTES:
% in development, needs to be better cleaned and commented
% inparg and Map structs are overlapping
% Updated 091110:
% Image intensities and slices can now be selected with sliders
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
%
% $Revision 1.0
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if nargin<3;
    sandbox = [];
end

%~~~~~~~~~~~~~~~~~~
% initate variables
%~~~~~~~~~~~~~~~~~~
% try
% inparg.alpha_value = 0.5;
% inparg.thr_high2 = inparg.thr_high*2;
% catch
%     inparg.thr_high2 = max(idat(:))*2;
%     inparg.thr_high = max(idat(:));
%     inparg.alpha_value = 0.5;
% end


idat2 = idat;
if ~isfield(inparg,'slicemin')
    inparg.slicemin = 1;
    inparg.slice_low = inparg.slicemin;
end

if ~isfield(inparg,'slicemax')
    inparg.slicemax = size(idat,3);
    inparg.slice_high = inparg.slicemax;
end

if ~isfield(inparg,'thr_high')

    if inparg.hoverlay==1;

        inparg.thr_high = round(nanmax(inparg.hidat(:)));
    else

        inparg.thr_high = round(nanmax(idat(:)));

    end
    
end

inparg.alpha_value = 0.5;
inparg.thr_high2 = inparg.thr_high*2;

if ~isfield(inparg,'thr_low')

    if inparg.hoverlay==1;
        inparg.thr_low = round(nanmin(inparg.hidat(:)));

    else
        inparg.thr_low = round(nanmin(idat(:)));


    end
    
end

if inparg.maptypenum ==4 || inparg.maptypenum == 5
    if ~isfield(inparg,'noiseplot');
        inparg.noiseplot = [7 7 size(idat,3)];
        inparg.noise_origin = [3 3];
        inparg.hnoise = 4;
    end


    % if size(idat,1)>3
    %     idat = mean(idat,4);
    % end

    noise = single(inparg.idat(inparg.noise(1):inparg.noise(2),inparg.noise(3):inparg.noise(4),end-3:end,:)); % Left to do: take only higher slices
    noise = noise(:);
    disp(['noise size = ' num2str(size(noise,1)) ' voxel']);
    inparg.noisemean = round(nanmean(noise*100))/100;
    inparg.noisemax = round(nanmax(noise*100))/100;
    inparg.noisestd = round(nanstd(noise*100))/100;
end

if ~isfield(inparg,'colormap');
    inparg.colormap = 1;
end

if ~isfield(inparg,'rot_slice');
    inparg.rot_slice = 0;
end

% figure(302);
% plot(noise);


% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Check for screen size
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scrsz = get(0,'ScreenSize');

if inparg.newmap == 1;
    slicemap = figure;
    %     set(slicemap,'Name','Activation Map','Position',[10 80 scrsz(3)-20 scrsz(4)-160] );
    set(slicemap,'Name',inparg.map_txt,'Position',[10 80 scrsz(3)-20 scrsz(4)-160] );
else
    slicemap = figure(103);
    set(slicemap,'Name',inparg.map_txt);
end
clf;


%~~~~~~~~~~~~~~~~~~
% Initiate Figure
%~~~~~~~~~~~~~~~~~~
slider_GUI = figure(301);
figure(slider_GUI); clf;
set(slider_GUI,'Position',[150,150,270,330],...
    'Resize','off','Toolbar','none','MenuBar','none',...
    'NumberTitle','off','Name','Display slices toolbox');


%~~~~~~~~~~~~~~~~~~
% Noise area
%~~~~~~~~~~~~~~~~~~

h_noise = uibuttongroup('visible','off','Position',[0.01 0.75 0.99 .25]);

u1 = uicontrol('Style','Radio','String','Area 1',...
    'pos',[5 30 100 30],'parent',h_noise,'HandleVisibility','off');
u2 = uicontrol('Style','Radio','String','Area 2',...
    'pos',[5 1 100 30],'parent',h_noise,'HandleVisibility','off');
u3 = uicontrol('Style','Radio','String','Area 3',...
    'pos',[100 30 100 30],'parent',h_noise,'HandleVisibility','off');
u4 = uicontrol('Style','Radio','String','Area 4',...
    'pos',[100 1 100 30],'parent',h_noise,'HandleVisibility','off');
u5 = uicontrol('Style','Radio','String','Area 5',...
    'pos',[200 30 100 30],'parent',h_noise,'HandleVisibility','off');
u6 = uicontrol('Style','Radio','String','None',...
    'pos',[200 1 100 30],'parent',h_noise,'HandleVisibility','off');


set(h_noise,'SelectionChangeFcn',@noise_area_Callback);
temp_selob = eval(['u' num2str(inparg.hnoise)]);
set(h_noise,'SelectedObject',temp_selob);
set(h_noise,'Visible','off');

rot_slice = uicontrol('Style','checkbox',...
    'String','Rotate',...
    'Position',[5,230,60,15],...
    'Callback',{@rotate_Callback});
%~~~~~~~~~~~~~~~~~~
% Show Noise values
%~~~~~~~~~~~~~~~~~~

if inparg.hnoise<6
    uicontrol('Style','text',...
        'String','Noise AVG',...
        'Position',[5,210,60,15]);

    uicontrol('Style','text',...
        'String',num2str(inparg.noisemean),...
        'Position',[75,210,40,15]);

    uicontrol('Style','text',...
        'String','SD',...
        'Position',[115,210,30,15]);

    uicontrol('Style','text',...
        'String',num2str(inparg.noisestd),...
        'Position',[145,210,40,15]);

    uicontrol('Style','text',...
        'String','Max',...
        'Position',[185,210,30,15]);

    uicontrol('Style','text',...
        'String',num2str(inparg.noisemax),...
        'Position',[220,210,30,15]);
end

%~~~~~~~~~~~~~~~~~~~~
% Overlay menu
%~~~~~~~~~~~~~~~~~~~~

h_pred_text = uicontrol('Style','text',...
    'String','Predictor',...
    'Position',[70,230,50,15]);

h_pred = uicontrol('Style','Edit',...
    'Position',[120,230,30,15],...
    'String',num2str(inparg.hrf_pred),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_predictor_Callback});

text_threshold = uicontrol('Style','text','String','Threshold : ',...
    'Position',[160,230,60,13]);

h_threshold = uicontrol('Style','edit','String',num2str(inparg.corparam),...
    'Position',[220,230,40,13],...
    'TooltipString','Select map threshold',...
    'Callback',{@Map_threshold_edit_Callback});

text_tsfx = uicontrol('Style','text',...
    'String','Scale x',...
    'Position',[5,160,35,15]);

h_tsfx = uicontrol('Style','Edit',...
    'Position',[40,160,30,15],...
    'String',num2str(inparg.tsfx),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_overlay_Callback});

text_tsfy = uicontrol('Style','text',...
    'String','Scale y',...
    'Position',[70,160,35,15]);

h_tsfy = uicontrol('Style','Edit',...
    'Position',[105,160,30,15],...
    'String',num2str(inparg.tsfy),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_overlay_Callback});

text_shiftx = uicontrol('Style','text',...
    'String','Shift x',...
    'Position',[135,160,35,15]);

h_shiftx = uicontrol('Style','Edit',...
    'Position',[170,160,30,15],...
    'String',num2str(inparg.shiftx),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_overlay_Callback});

text_shifty = uicontrol('Style','text',...
    'String','Shift y',...
    'Position',[200,160,30,15]);

h_shifty = uicontrol('Style','Edit',...
    'Position',[235,160,30,15],...
    'String',num2str(inparg.shifty),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_overlay_Callback});

%~~~~~~~~~~~~~~~~~~~~
% Rayleigh menu
%~~~~~~~~~~~~~~~~~~~~

h_rayleigh = uicontrol('Style','Radiobutton','String','Rayleigh',...
    'Position',[5,185,70,15],...
    'TooltipString','Do Rayleigh correction for SNR maps',...
    'Callback',{@rayleigh_correction_Callback});

%~~~~~~~~~~~~~~~~~~~~
% Color menu
%~~~~~~~~~~~~~~~~~~~~

% uicontrol('Style','text','String','Color',...
%     'Position',[1,137,50,15]);

uicontrol('Style','popupmenu',...
    'String',{'Gray','Hot','Jet','HSV','Cool','Lines','Copper','Flag','Prism','Bone'},...
    'Position',[90,180,50,25],...
    'Value',inparg.colormap,...
    'TooltipString','Select a color scheme for display',...
    'Callback',{@Colormap_Selection_Callback});

%~~~~~~~~~~~~~~~~~~
% Replicate diagnostics here
%~~~~~~~~~~~~~~~~~~

uicontrol('Style','popupmenu',...
    'String',{'Scaninfo','Plot slices','Voxel Time Course','SNR Map',...
    'ROI SNR Map','CV Map','TN Map','STD Map','Z-score Map','Special Map',...
    'AB Map','Ghost Map','Noise Time Course','Sandmap'},...
    'Value',inparg.maptypenum,...
    'Position',[170,180,80,25],...
    'TooltipString','Select diagnostic method here',...
    'Callback',{@Slice_Display_Callback});

%~~~~~~~~~~~~~~~~~~
% Sliders
%~~~~~~~~~~~~~~~~~~

uicontrol('Style','Text','String','Map Transparency',...
    'Position',[5,140,250,15]);

h_adjust_alpha = uicontrol('Style','Slider',...
    'Position',[5,125,250,15],...
    'Min',0,'Max',1,'Value',inparg.alpha_value,...
    'SliderStep',[0.01 0.1],...
    'TooltipString','Select and preview transparency',...
    'Callback',{@adjust_slice_Callback});

uicontrol('Style','Text','String','Slice Low',...
    'Position',[5,80,250,15]);

uicontrol('Style','Text','String','Slice High',...
    'Position',[5,110,250,15]);

h_adjust_lim_slice_low = uicontrol('Style','Slider',...
    'Position',[5,65,200,15],...
    'Min',inparg.slice_low,'Max',inparg.slice_high,'Value',inparg.slice_low,...
    'SliderStep',[0.01 0.1],...
    'TooltipString','Select and preview low threshold',...
    'Callback',{@adjust_slice_Callback});

h_adjust_lim_slicenum_low = uicontrol('Style','Edit',...
    'Position',[205,65,50,15],...
    'String',num2str(inparg.slice_low),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_lim_Callback});

h_adjust_lim_slice_high = uicontrol('Style','Slider',...
    'Position',[5,95,200,15],...
    'Min',inparg.slice_low,'Max',inparg.slice_high,'Value',inparg.slice_high,...
    'SliderStep',[0.01 0.1],...
    'TooltipString','Select and preview high threshold',...
    'Callback',{@adjust_slice_Callback});

h_adjust_lim_slicenum_high = uicontrol('Style','Edit',...
    'Position',[205,95,50,15],...
    'String',num2str(inparg.slice_high),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_lim_Callback});

uicontrol('Style','Text','String','Low',...
    'Position',[5,20,250,15]);

uicontrol('Style','Text','String','High',...
    'Position',[5,50,250,15]);

h_adjust_lim_slid_low = uicontrol('Style','Slider',...
    'Position',[5,5,200,15],...
    'Min',inparg.thr_low,'Max',inparg.thr_high,'Value',inparg.thr_low,...
    'SliderStep',[0.01 0.1],...
    'TooltipString','Select and preview low threshold',...
    'Callback',{@adjust_slice_Callback});

h_adjust_lim_num_low = uicontrol('Style','Edit',...
    'Position',[205,5,50,15],...
    'String',num2str(inparg.thr_low),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_lim_Callback});

h_adjust_lim_slid_high = uicontrol('Style','Slider',...
    'Position',[5,35,200,15],...
    'Min',inparg.thr_low,'Max',inparg.thr_high2,'Value',inparg.thr_high,...
    'SliderStep',[0.01 0.1],...
    'TooltipString','Select and preview high threshold',...
    'Callback',{@adjust_slice_Callback});

h_adjust_lim_num_high = uicontrol('Style','Edit',...
    'Position',[205,35,50,15],...
    'String',num2str(inparg.thr_high),...
    'TooltipString','Edit and preview high threshold',...
    'Callback',{@edit_lim_Callback});


figure(103);
display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg);
%fsb_map_browser(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg);


%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Subfunctions
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Main plotting function
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function display_image(idat,thr_low,thr_high,inparg)
        if inparg.hoverlay == 1;
            set(h_tsfx,'Visible','on')
            set(h_tsfy,'Visible','on')
            set(h_shiftx,'Visible','on')
            set(h_shifty,'Visible','on')
            set(text_tsfx,'Visible','on')
            set(text_tsfy,'Visible','on')
            set(text_shiftx,'Visible','on')
            set(text_shifty,'Visible','on')
        else
            set(h_tsfx,'Visible','off')
            set(h_tsfy,'Visible','off')
            set(h_shiftx,'Visible','off')
            set(h_shifty,'Visible','off')
            set(text_tsfx,'Visible','off')
            set(text_tsfy,'Visible','off')
            set(text_shiftx,'Visible','off')
            set(text_shifty,'Visible','off')
        end
        if inparg.maptypenum == 4
            set(h_noise,'Visible','on');
        else
            set(h_noise,'Visible','off');
        end
        if inparg.calcorr == 1;
            set(h_pred_text,'Visible','on');
            set(h_pred,'Visible','on')
            set(h_threshold,'Visible','on')
            set(text_threshold,'Visible','on')
        else
            set(h_pred_text,'Visible','off');
            set(h_pred,'Visible','off')
            set(h_threshold,'Visible','off')
            set(text_threshold,'Visible','off')
        end
         if inparg.rc == 99
            Map.rc=99;
        end

        fsb_map_browser(idat,thr_low,thr_high,inparg)

    end

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Map threshold callback
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function  Map_threshold_edit_Callback(source,eventdata)

        inparg.corparam = str2num(get(h_threshold,'string'));
        %corparams = sprintf('%1.4f',Map.corparam);
%        set(h_calcorrelation2_slid,'Value');
%        set(h_probability_slid,'String',probstring);
        disp([' Map threshold ' num2str(inparg.corparam) ]);

        display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)

    end
% 
%         % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%         % Check for number of slices
%         % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% 
%         figure(103); clf;
%         dim = size(idat);
% 
%         if size(dim,2)>2
%             nslices = dim(3);
%         else
%             nslices = 1;
%         end
% 
%         %~~~~~~~~~~~~~
%         % Calculate mean, std, min, max images for colormap
%         %~~~~~~~~~~~~~
% 
%         imean = mean(idat,4);
%         meanimage = mean(imean(:));
%         stdimage = std(imean(:));
%         minimage = min(imean(:));
%         maximage = meanimage+5*stdimage;
%         maximage2 = max(imean(:));
% 
%         if maximage2<maximage ;
%             maximage = maximage2;
%         end
% 
%         if inparg.nscal == 1
%             maximage = meanimage-0.5*meanimage;
%         end
% 
%         if thr_low==1 && thr_high==maximage2
%             clims = [minimage maximage];
%         else
%             clims = [thr_low thr_high];
%         end
% 
%         axis off;
% 
%         % ~~~~~~~~~~~~~~~~~~~~~~~~
%         % Figure out necessary size of plots
%         % ~~~~~~~~~~~~~~~~~~~~~~~~
%         size1 = ceil(sqrt(size(idat,3)));
%         size2 = round(sqrt(size(idat,3)));
% 
%         while nslices == size1*size2 % If it does not fit, use a bigger size
%             size2 = size2+1;
%         end
% 
%         % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%         % Go through slices and plot data
%         % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%         figure (slicemap);
% 
%         for x= 1:nslices+1;
%             h(x) = subplot(size1,size2,x);
%             cla;
%         end
% 
%         for x= 1:nslices
% 
%             p(x,:) = get(h(x), 'position');
%             set(h(x), 'position', [((p(x,1)-0.115)*1.2) ((p(x,2)-0.055)*1.085) (p(x,3)*1.4) (p(x,4)*1.3)]);
% 
%             if inparg.rot_slice == 0;
%                 slicedata = idat(:,:,x);
%             else
%                 slicedata = flipud(idat(:,:,x)');
%             end
% 
%             if inparg.hoverlay==1;
%                 if inparg.rot_slice == 0;
%                     slicedata = inparg.hidat(:,:,x);
%                 else
%                     slicedata = flipud(inparg.hidat(:,:,x)');
%                 end
%             end
% 
%             subplot(h(x));
%             try
%                 imagesc(slicedata,[thr_low thr_high]);
%             catch
%                 disp('Image data only zeros, aborting...')
%             end
%             if inparg.maptypenum==4
%                 %~~~~~~~~~~~~~~~~~~~~~~~
%                 % Plot SNR Noise area
%                 %~~~~~~~~~~~~~~~~~~~~~~~
%                 set(h_noise,'Visible','on');
%                 if ~isfield(inparg,'noisetd')
%                     noisetd = [size(idat,3)-3 size(idat,3)];
%                 else
%                     noisetd = inparg.noisetd;
%                 end
% 
%                 if x>noisetd(1)&&x<=noisetd(2)
%                     noiseborder = [inparg.noise(1),inparg.noise(3),inparg.noise(4)-inparg.noise(3),inparg.noise(2)-inparg.noise(1)];
%                     noiseborder(noiseborder==0)=1;
%                     %rectangle('Position',[inparg.noise_origin(1),inparg.noise_origin(2),inparg.noiseplot(1),inparg.noiseplot(2)],'EdgeColor','g');
%                     rectangle('Position',noiseborder,'EdgeColor','g');
%                 end
%             end
%             %~~~~~~~~~~~~~~~~~~~~~~~
%             % Plot slice number
%             %~~~~~~~~~~~~~~~~~~~~~~~
% 
%             hold on;
%             axis off;
%             axis xy;
% 
%             % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%             % Check if activation map needs to be overlaid
%             % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%             if  inparg.calcorr ==1
% 
%                 if inparg.rot_slice == 0;
%                     Map.z = (squeeze(inparg.dummy(:,:,x,inparg.hrf_pred)));
%                 else
%                     Map.z = flipud(squeeze(inparg.dummy(:,:,x,inparg.hrf_pred)'));
%                 end
%                 Map.corparam = inparg.corparam;
%                 Map.probparam = inparg.probparam;
%                 Map.rc = inparg.rc;
% 
%                 Map = fsb_colmap(Map);
% 
%                 % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%                 % Check if map needs to be overlaid on high-res brain
%                 % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%                 if inparg.hoverlay == 1;
%                     set(h_tsfx,'Visible','on')
%                     set(h_tsfy,'Visible','on')
%                     set(h_shiftx,'Visible','on')
%                     set(h_shifty,'Visible','on')
%                     if inparg.rot_slice==0
%                         Map.slice_data = inparg.slicez_data;
%                     else
%                         Map.slice_data = flipud(inparg.slicez_data');
%                     end
%                     Map.tsfx = inparg.tsfx;
%                     Map.tsfy = inparg.tsfy;
%                     Map.shiftx = inparg.shiftx;
%                     Map.shifty = inparg.shifty;
%                     Map = fsb_map_overlay(Map);
% 
%                 else
%                     set(h_tsfx,'Visible','off')
%                     set(h_tsfy,'Visible','off')
%                     set(h_shiftx,'Visible','off')
%                     set(h_shifty,'Visible','off')
%                 end
%                 % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%                 % Plot overlay
%                 % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%                 plot_z1 = image(Map.zm);
%                 Map.tzi = single(Map.tzi)*inparg.alpha_value;
%                 set (plot_z1,'AlphaData',Map.tzi);
%                 hold off;
% 
%                 if x>1;
%                     subplot(h(x-1));
% 
%                 end
% 
%             end
%         end
% 
%         % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%         % Plot color bars
%         % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% 
%         p(x+1,:) = get(h(x+1), 'position');
%         set(h(x+1), 'position', [((p(x+1,1)-0.115)*1.2) ((p(x+1,2)-0.055)*1.085) (p(x+1,3)*1.4) (p(x+1,4)*1.15)]);
% 
%         hold on;
%         subplot(h(x+1));
% 
%         imagesc(idat(:,:,1),clims);
%         colorbar('location','West');
%         axis off; cla;
% 
%         if  inparg.calcorr ==1
%             h(x+1) = subplot(size1,size2,x+1);
%             p(x+1,:) = get(h(x+1), 'position');
%             set(h(x+1), 'position', [((p(x+1,1)-0.05)*1.2) ((p(x+1,2)-0.055)*1.085) (p(x+1,3)*0.12) (p(x+1,4)*1.3)]);
%             try
%                 cm2 = flipud(Map.cm);
%                 cm2 = repmat(cm2,[1 1 2]);
%                 cm2 = permute(cm2,[1 3 2]);
%                 image(cm2);
%                 set(gca,'YTick',1:25:101);
%                 set(gca,'YTickLabel',[1 0.5  0 -0.5  -1],'FontSize',10);
%                 set(gca,'XTickLabel',' ');
%             catch
%                 cla;
%             end
%         else
%             axis off; cla;
%         end
% 
%         % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%         % This is for simultaneous display of colorbars for the map and the
%         % underlying image - more work to be done
%         %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% 
%         % h(x+2) = subplot(size1,size2,x+2);
%         % p(x+2,:) = get(h(x+2), 'position');
%         % set(h(x+2), 'position', [((p(x+1,1)-0.115)*1.2) ((p(x+1,2)-0.055)*1.085) (p(x+1,3)*1.4) (p(x+1,4)*1.3)]);
%         % % hold on;
%         % imagesc(idat(:,:,1)',clims);
%         % colorbar;
%         % axis off; cla;
% 
%         %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%         % Plot additional information
%         %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% 
%         if x<19 && inparg.calcorr == 1
%             try
%                 subplot(size1,size2,x+2); axis off;
%                 text(0.2,0.9,'Map thresholds:');
%                 text(0.2,0.7,inparg.corrstring);
%                 text(0.2,0.5,inparg.probstring);
%             catch
%                 disp(['Map thresholds:' inparg.corrstring inparg.probstring]);
%             end
%         end
% 
%     end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Rotation handling
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    function rotate_Callback(source,eventdata)

        if (get(rot_slice,'Value') == get(rot_slice,'Max'))
            inparg.rot_slice = 1;
        else
            inparg.rot_slice = 0;
        end
        display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
        %fsb_map_browser(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Colormap handling
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    function Colormap_Selection_Callback(source,eventdata)

        str = get(source,'String');
        val = get(source,'Value');

        colormap(str{val});
        inparg.colormap = val;
        %figure(113); colormap(str{val});
        figure(103); colormap(str{val});

    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Colormap handling
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function edit_predictor_Callback(source,eventdata)

        str = get(source,'String');
        %val = get(source,'Value');
        %inparg.hrf_pred = str(val);
        inparg.hrf_pred = str2double(str);
        display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% set thresholds
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function adjust_slice_Callback(source,eventdata)

        slicemax =  floor(get(h_adjust_lim_slice_high,'Value'));
        slicemin =  floor(get(h_adjust_lim_slice_low,'Value'));

        if slicemax<=slicemin
            slicemax = slicemin+1;
        end

        inparg.slicemax = slicemax;
        inparg.slicemin = slicemin;


        inparg.alpha_value = get(h_adjust_alpha,'Value');
        thr_high =  floor(get(h_adjust_lim_slid_high,'Value'));
        thr_low =  floor(get(h_adjust_lim_slid_low,'Value'));

        if thr_high<=thr_low
            thr_high = thr_low+1;
        end

        inparg.thr_high = thr_high;
        inparg.thr_low = thr_low;

        set(h_adjust_lim_num_high,'String',num2str(inparg.thr_high));
        set(h_adjust_lim_num_low,'String',num2str(inparg.thr_low));
        set(h_adjust_lim_slicenum_high,'String',num2str(inparg.slicemax));
        set(h_adjust_lim_slicenum_low,'String',num2str(inparg.slicemin));

        display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
        %fsb_map_browser(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)

    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% edit threshold manually
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function edit_lim_Callback(source,eventdata)
        thr_high = get(h_adjust_lim_num_high,'String');
        thr_low = get(h_adjust_lim_num_low,'String');
        slice_high = get(h_adjust_lim_slicenum_high,'String');
        slice_low = get(h_adjust_lim_slicenum_low,'String');
        inparg.thr_high = str2double(thr_high);
        inparg.thr_low = str2double(thr_low);
        inparg.slicemax = str2double(slice_high);
        inparg.slicemin = str2double(slice_low);
        set(h_adjust_lim_slid_high,'Value',inparg.thr_high);
        set(h_adjust_lim_slid_low,'Value',inparg.thr_low);
        set(h_adjust_lim_slice_high,'Value',inparg.slicemax);
        set(h_adjust_lim_slice_low,'Value',inparg.slicemin);
        display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
        %fsb_map_browser(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)

    end
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Do rayleigh correction if desired
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function rayleigh_correction_Callback(source,eventdata)
        % added the 0.655 factor to account for Rayleigh distribution
        % 021110 see also
        % http://www.revisemri.com/questions/equip_qa/measuring_snr

        if (get(h_rayleigh,'Value') == get(h_rayleigh,'Max')) ;
            idat = 0.655.*idat2;
        else
            idat = idat2;
        end
        display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
        %fsb_map_browser(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% redefine noise area
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function noise_area_Callback(source,eventdata)

        str = get(h_noise, 'SelectedObject');
        noise_a = get(str,'String');
        noise_a = noise_a(end);
        noise_a = str2double(noise_a);

        switch noise_a

            case 1 % Noise area front left
                inparg.noiseplot = [10 10 size(idat,3)];
                inparg.noise_origin = [3 3];
                inparg.noise = [3 10 3 10];
                inparg.hnoise = 1;
                inparg.noisetd = [size(idat,3)-3 size(idat,3)];

            case 2 % Noise area back left
                inparg.noiseplot = [7 7 size(idat,3)];
                inparg.noise_origin = [size(idat,1)-10 3];
                inparg.noise = [size(idat,1)-10 size(idat,2)-3 3 10];
                inparg.hnoise = 2;
                inparg.noisetd = [size(idat,3)-3 size(idat,3)];

            case 3 % Noise area side
                inparg.noiseplot = [3 size(idat,2)-3];
                inparg.noise_origin = [3 3];
                inparg.noise = [3 size(idat,2)-3 3 3];
                inparg.hnoise = 3;
                inparg.noisetd = [size(idat,3)-3 size(idat,3)];

            case 4 % Noise area front
                inparg.noiseplot = [size(idat,2)-3 3];
                inparg.noise_origin = [3 3];
                inparg.noise = [3 3 3 size(idat,2)-3];
                inparg.hnoise = 4;
                inparg.noisetd = [size(idat,3)-3 size(idat,3)];

            case 5   % Noise area ROI
                inparg.hnoise = 5;
                inparg.noise_origin(1) = inparg.slice_n(2)-inparg.roi;
                inparg.noise_origin(2) = inparg.slice_n(1)-inparg.roi;
                inparg.noiseplot(1) = inparg.roi*2;
                inparg.noiseplot(2) = inparg.roi*2;
                inparg.noise = [inparg.slice_n(2)-inparg.roi inparg.slice_n(2)+inparg.roi inparg.slice_n(1)-inparg.roi inparg.slice_n(1)+inparg.roi];
                inparg.noisetd = [inparg.slice_n(3)-inparg.roi inparg.slice_n(3)+inparg.roi];

            case 6   % No noise area
                inparg.hnoise = 6;

        end
        fsb_diag(inparg.idat,inparg.imean,sandbox,inparg);
        %display_image(idat(:,:,slicemin:slicemax),thr_low,thr_high,inparg)
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Diagnostics
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function Slice_Display_Callback(source, eventdata)

        try
            str = get(source, 'String');
            val = get(source,'Value');
            inparg.maptype = str{val};
            Map.maptype = inparg.maptype;
        catch
            inparg.maptype = 'Plot slices';
            Map.maptype = inparg.maptype;
        end

        % reset threshold values

        inparg.maptype = Map.maptype;
        idat = inparg.idat;
        imean = inparg.imean;
        inparg = rmfield(inparg, 'slicemin');
        fsb_diag(idat,imean,sandbox,inparg);
    end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Edit overlay fit
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    function edit_overlay_Callback(source,eventdata)
        inparg.tsfx = str2num(get(h_tsfx,'String'));
        inparg.tsfy = str2num(get(h_tsfy,'String'));
        inparg.shiftx = str2num(get(h_shiftx,'String'));
        inparg.shifty = str2num(get(h_shifty,'String'));
        display_image(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
        %fsb_map_browser(idat(:,:,inparg.slicemin:inparg.slicemax),inparg.thr_low,inparg.thr_high,inparg)
    end


end


